package cn.wolfcode.web.controller;

import cn.wolfcode.ssm.domain.Permission;
import cn.wolfcode.ssm.page.JsonPage;
import cn.wolfcode.ssm.page.PageResult;
import cn.wolfcode.ssm.query.QueryObject;
import cn.wolfcode.ssm.service.IPermissionService;
import cn.wolfcode.ssm.util.requiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")

public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") QueryObject qo){
        PageInfo<Permission> pageResult = permissionService.query(qo);
        model.addAttribute("pageResult",pageResult);
        return "/permission/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null){
            permissionService.delete(id);
        }
        return "redirect:/permission/list.do";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id){
        if (id != null){
            Permission permission = permissionService.get(id);
            model.addAttribute("permission",permission);
        }
        return "/permission/input";
    }

    @requiredPermission(name = "权限增改",expression = "permission:LLL")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission, Long id){
        if (id != null){
            permissionService.update(permission);
        }else{
            permissionService.save(permission);
        }
        return "redirect:/permission/list.do";
    }

    @RequestMapping("/reload")
    @ResponseBody
    public JsonPage reload() {
        try {
            permissionService.reload();
            return new JsonPage();
        }catch(Exception e){
            e.printStackTrace();
        }
        return new JsonPage(false,"加载失败");
    }
}
